package main.java.br.com.projeto.application.managedbeans;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ComponentSystemEvent;

import main.java.br.com.projeto.domain.entity.Grupo;
import main.java.br.com.projeto.domain.service.GrupoService;

import org.primefaces.context.RequestContext;

@ManagedBean
@ViewScoped
public class GrupoBean implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -313788601381401910L;

	@ManagedProperty(value = "#{grupoService}")
	private GrupoService grupoService;

	private Grupo currentGrupo;
	
	private List<Grupo> grupos;
	
	private String filterId;
	
	private String filter;
	
	private Date date;
	
	private String acao;
	
	public GrupoBean() {
		// TODO Auto-generated constructor stub
	}
	
	/*-------------------------------------------------------------------
	 *				 		   BEHAVIORS
	 *-------------------------------------------------------------------*/
	
	public void initialize(ComponentSystemEvent event) {
		if(!FacesContext.getCurrentInstance().isPostback()) {
			this.fillGrupos();
		}
	}
	
	public void initializeForm(Long grupoId) 
	{
		if(!FacesContext.getCurrentInstance().isPostback()) 
		{
			if (grupoId != null && grupoId > 0) {
				this.currentGrupo = this.grupoService.findGrupoById(grupoId);
				this.acao = "Editar Grupo";
			} else if (this.currentGrupo == null) {
				this.setCurrentGrupo(new Grupo());
				this.acao = "Cadastrar Grupo";
			}
		}
	}
	
	public void fillGrupos() {
		this.grupos = this.grupoService.listAll();
	}
	
	public void save() {
		try
		{
			if ( this.currentGrupo.getId() == null || this.currentGrupo.getId() == 0 )
			{
				this.currentGrupo = this.grupoService.insertGrupo(this.getCurrentGrupo());
			}
			else
			{
				this.currentGrupo = this.grupoService.updateGrupo(this.getCurrentGrupo());
			}
			
			FacesContext.getCurrentInstance().addMessage( null, new FacesMessage( FacesMessage.SEVERITY_INFO, "Grupo salvo com sucesso!", "" ) );
			FacesContext.getCurrentInstance().getExternalContext().getFlash().setKeepMessages(true);
			
			this.changeToDetail();
		}
		catch ( Exception e )
		{
			e.printStackTrace();
		}			
	}
	
	public void remove (ActionEvent event) {
		try
		{
			this.grupoService.removeGrupo(this.getCurrentGrupo());
			this.fillGrupos();
			RequestContext.getCurrentInstance().update("tableGrupo");
		}
		catch ( Exception e )
		{
			e.printStackTrace();
			FacesContext.getCurrentInstance().addMessage( null, new FacesMessage( FacesMessage.SEVERITY_ERROR, e.getMessage(), "" ) );
		}
	}
	
	public void fillByFilter() {
		if (this.filterId.equals("")){
			this.filterId = "0";
		}
		this.grupos = this.grupoService.findByFilter(Long.parseLong(filterId), filter);
		this.filterId = "";
		RequestContext.getCurrentInstance().update("tableGrupo");
	}
	
	public void removeFromDetail()
	{
		try 
		{
			this.grupoService.removeGrupo( this.currentGrupo );
			
			RequestContext.getCurrentInstance().execute("popupConfirmDeleteDetail.hide()");
			RequestContext.getCurrentInstance().execute("completeDeletePopup.show()");
			
		} 
		catch (Exception e) 
		{
			FacesContext.getCurrentInstance().addMessage( null, new FacesMessage( FacesMessage.SEVERITY_ERROR, e.getMessage(), "" ) );
		}
	}
	/*-------------------------------------------------------------------
	 *				 		   NAVIGATION
	 *-------------------------------------------------------------------*/
	public String changeToNew() {
		return "/views/grupo/formGrupo?faces-redirect=true";
	}

	public String changeToList() {
		return "/views/grupo/listGrupo?faces-redirect=true";
	}
	
	public String changeToEdit(Long id) {
		return "/views/grupo/formGrupo?faces-redirect=true&grupoId="+id;
	}
	
	public void changeToDelete(Long id) {
		this.currentGrupo = this.grupoService.findGrupoById(id);
		RequestContext.getCurrentInstance().execute("popupConfirmDelete.show()");
	}	
	
	public void changeToDetail() {
		try {
			String url = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();
			url += "/views/grupo/detailGrupo.xhtml?grupoId="+this.currentGrupo.getId();
			
			FacesContext.getCurrentInstance().getExternalContext().redirect(url);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String changeToEdit()
	{
		return "/views/grupo/formGrupo?faces-redirect=true&grupoId="+this.currentGrupo.getId();	
	}
	/*-------------------------------------------------------------------
	 *				 		   GETTERS AND SETTERS
	 *-------------------------------------------------------------------*/
	
	public List<Grupo> getGrupos() {
		return grupos;
	}
	
	public void setGrupos(List<Grupo> grupos) {
		this.grupos = grupos;
	}
	
	public GrupoService getGrupoService() {
		return grupoService;
	}
	
	public void setGrupoService(GrupoService grupoService) {
		this.grupoService = grupoService;
	}

	public Grupo getCurrentGrupo() {
		return currentGrupo;
	}

	public void setCurrentGrupo(Grupo currentGrupo) {
		this.currentGrupo = currentGrupo;
	}

	public String getfilterId() {
		return filterId;
	}

	public void setfilterId(String filterId) {
		this.filterId = filterId;
	}

	public String getFilter() {
		return filter;
	}

	public void setFilter(String filter) {
		this.filter = filter;
	}

	public Date getDate() {
		this.date = new Date();
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public String getFilterId() {
		return filterId;
	}

	public void setFilterId(String filterId) {
		this.filterId = filterId;
	}

	public String getAcao() {
		return acao;
	}

	public void setAcao(String acao) {
		this.acao = acao;
	}
	
}
